@extends('layouts.app')

@section('title', '用户列表')

@section('css-import')
<style type="text/css">
	.page-s{
		display: inline-block;
		padding-bottom: 0px;
		margin: 29px 9px;
		border-radius: 4px;
		float:  right;
	}
</style>
@endsection

@section('content-fluid')
<div class="row">
	<!-- BASIC TABLE -->
	<div class="panel">
		<div class="panel-heading">
			<h3 class="panel-title">乐享用户</h3>
		</div>
		<div class="panel-body">
			
			<form action="/userList" method="get">
				<ul class="nav nav-pills" style="float:left;">
				  <li role="presentation" class="orderall">
				  	<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
				      搜索条件 <span class="caret"></span>
				    </a>
				    <ul class="dropdown-menu">
				      <li role="presentation" class="yiqucan {{$request->input('t')=='name'?'active':''}}"><a href="javascript:;">昵称</a></li>
				      <li role="presentation" class="weiqucan {{$request->input('t')=='tel'?'active':''}}"><a href="javascript:;">手机号</a></li>
				    </ul>
				  </li>
				</ul>
				<div class="input-group col-md-4 pull-right" style="z-index: auto;">

					<input type="text" value="{{$_GET['t'] ?? ''}}" class="form-control app-search-text" name="s" AUTOCOMPLETE="off" placeholder="搜索用名">
					<input type="hidden" value="name" name="t">
					@foreach($_GET as $k=>$v)
						@if($k!='page' && $k!='t')
						<input type="hidden" value="{{$v}}" name="{{$k}}">
						@endif
					@endforeach
					<span class="input-group-btn"><button type="submit" class="btn btn-primary search-jslx"><i class="fa fa-search"></i></button></span>
				</div>
			</form>
			<table class="table table-hover">
				<thead>
					<tr>
						<th>#</th>
						<th>昵称</th>
						<th>手机号</th>
						<th>账户余额</th>
						<th>积分</th>
						<th>注册时间</th>
						<th>购卡推荐人/次数</th>
						<th>配送</th>
						<th>商圈</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					@foreach($users as $k=>$v)
					<tr>
						<td>{{$v->user_id}}</td>
						<td class="nickname">{{$v->nicename ?? '未填写'}}</td>
						<td>{{$v->tel}}</td>
						<td>{{$v->account}}</td>
						<td>{{$v->integral}}</td>
						<td>{{date('Y年m月d日 H:i',$v->insert_time)}}</td>
						<td><a href="/user-details?userid={{$v->share_userid}}">{{$v->share_user_name}}</a>({{$card->orderCount($v->user_id)}})</td>
						<td class="circlename">{{$v->circle_name}}</td>
						<td>{{$v->sucircle_name}}</td>
						<td>
							<a href="/user-details?userid={{$v->user_id}}"><i class="fa fa-info-circle"></i> 查看</a>
							<a href="javascript:void(0);" onclick="setpeisong(this,'{{$v->user_id}}')"><i class="fa fa-info-circle"></i> 设为配送员</a>
						</td>
					</tr>
					@endforeach
				</tbody>
			</table>
			{{$users->appends(['circle_id' => $_GET['circle_id'] ?? '','shop_name'=>$_GET['shop_name'] ?? '','t'=>$_GET['t'] ?? '','s'=>$_GET['s'] ?? ''])->links()}}
			<div class="page-s">本页共{{ $users->count() }}条</div>
			<div class="page-s">共{{ $users->total() }}条</div>
			<div class="page-s">当前第{{ $users->currentPage() }}页</div>
			<div class="page-s">共{{ $users->lastPage() }}页</div>
		</div>
	</div>
	<!-- END BASIC TABLE -->
</div>
<div class="modal fade" tabindex="-1" id="exampleModal" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">设置商圈</h4>
      </div>
      <form action="/edit/user/circle" method="post" id="circleEditForm">
	      <div class="modal-body">
	      	    <div class="input-group " style="z-index: auto;">
					<input type="text" value="" class="form-control app-search-text" name="shop_name" AUTOCOMPLETE="off" placeholder="搜索商圈">
					<span class="input-group-btn"><button type="submit" class="btn btn-primary search-jslx"><i class="fa fa-search"></i></button></span>
				</div>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	        <button type="submit" class="btn btn-primary saveCateImage">保存</button>
	      </div>
      </form>
    </div>
  </div>
</div>
@endsection

@section('js-write')
<script type="text/javascript">
	$('.weiqucan').click(function(){
			$('input[name="t"]').val('tel');
			$('input[name="s"]').attr('placeholder','请输入手机号');
		})
	$('.yiqucan').click(function(){
			$('input[name="t"]').val('name');
			$('input[name="s"]').attr('placeholder','请输入昵称');
		})
	function setpeisong(this_l,userid) {
		var circlename = $(this_l).parents('tr').find('.circlename').html();
		if(circlename=='未设置'){
			circlename = '';
		}
		var this_ = $('<div class="input-group " style="z-index: auto;"><input type="text" value="'+circlename+'" class="form-control app-search-text" name="circle_name" AUTOCOMPLETE="off" placeholder="搜索商圈"><span class="input-group-btn"><button type="button" class="btn btn-primary search-jslx"><i class="fa fa-search"></i></button></span></div>');
		var modal = $('#exampleModal');
		modal.find('.modal-body').empty();
		modal.find('.modal-title').html($(this_l).parents('tr').find('.nickname').html()+'商圈修改');
	    modal.find('.modal-body').append($(this_));
	    modal.find('.modal-body').append($('<input type="hidden" name="user_id" value="'+userid+'">'));
	    if(!modal.find('.saveCateImage').length){
	    	modal.find('.modal-footer').append($('<button type="submit" class="btn btn-primary saveCateImage">保存</button>'));
	    }
		$('#exampleModal').modal();
		$('.search-jslx').click(function(){
			$.ajaxSetup({
			    headers: {
			        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			    }
			});
			$.post('/search/circle',{'circle_name':$('#exampleModal').find('input[name="circle_name"]').val()},function(response){
				if(response.success){
					$('#exampleModal').find('.modal-body').append($('<hr><p class="lead"><span class="label label-info shop-info-name">'+response.data.circle_name+'</span></p> <hr>'));
					$('#circleEditForm').append($('<input type="hidden" name="circle_id" value="'+response.data.circle_id+'">'));
				}else{
					toastr.error(response.message);
				}
			},'json');
		});
	}
	var CouponSendParam = {
	    'dataType':'json',
	    'beforeSubmit':CouponBefore,
	    'success':CouponResponse,
	    'error':toutiaoError,
	};
	function CouponBefore(formData, jqForm, options){
	    
	}
	function CouponResponse(response) {
	    if(response.success){
	        setTimeout(function(){
	            location.href = location.href;
	        },2000);
	        toastr.success(response.message);
	    }else{
	        toastr.error('修改失败，请联系开发人员！');
	    }
	}

	function toutiaoError(XMLHttpRequest, textStatus, errorThrown) {
		if(XMLHttpRequest.status==422){
			var RegError = XMLHttpRequest.responseJSON.errors;
			for(var o in RegError){
				for (var i = 0; i < RegError[o].length; i++) {
					toastr.error(RegError[o][i]);
				}
			}
		}
	}
	ajaxForm_('#circleEditForm', CouponSendParam);
</script>
@endsection